const Base = require("./base.js");
const cachePre = "NEWS_";
const modelName = "news";
module.exports = class extends Base {
  /**
   * 列表
   * @param name
   * @returns {Promise<any>}
   */
  async indexAction() {
    const sT = Date.now();
    const post = this.post();
    const db = this.model(modelName);
    const result = {};
    let data;
    const cacheKey = `${cachePre}list_${think.md5(JSON.stringify(post))}`;
    const cacheService = this.service("cache/key");
    post &&
      post.cache &&
      (await cacheService.set(cacheKey, JSON.stringify(post)));
    data = await this.cache(cacheKey);
    result.errno = 0;
    if (post && post.cache && data && data.count) {
      result.data = data;
      result.cache = true;
    } else {
      data = await db.list(post);
      await this.cache(cacheKey, data);
      result.data = data;
    }
    result.time = Date.now() - sT;
    return this.json(this.format(result));
  }

  /**
   * 删除
   * @param id
   * @returns {Promise<void>}
   */
  async delAction() {
    const post = this.post();
    const sT = Date.now();
    const db = this.model("news");
    const dbContent = this.model("news_content");
    let result = {};
    try {
      await db.delete(post.id);
      await dbContent.where({ news_id: post.id }).delete();
      result.errno = 0;
    } catch (e) {
      result.errno = -1;
      result.errmsg = e.toString();
    }
    result.time = Date.now() - sT;
    return this.json(this.format(result));
  }

  /**
   * 添加
   * @returns {Promise<any>}
   */
  async addAction() {
    const sT = Date.now();
    const post = this.post();
    let result = {};
    try {
      result.data = await this.save(post);
      result.errno = 0;
    } catch (e) {
      result.errno = -1;
      result.errmsg = e.toString();
    }
    result.time = Date.now() - sT;
    return this.json(this.format(result));
  }

  /**
   * 详情
   * @returns {Promise<any>}
   */
  async detailAction() {
    const sT = Date.now();
    const post = this.post();
    const db = this.model("news");
    const result = {};
    try {
      result.errno = 0;
      result.data = await db.detail(post);
      if (post.id) await db.where({ id: post.id }).increment("hits", 1);
    } catch (e) {
      result.errno = -1;
      result.errmsg = e.toString();
    }
    result.time = Date.now() - sT;
    return this.json(this.format(result));
  }

  /**
   * 修改
   * @returns {Promise<any>}
   */
  async updateAction() {
    const sT = Date.now();
    const post = this.post();
    let result = {};
    try {
      result.data = await this.save(post);
      result.errno = 0;
    } catch (e) {
      result.errno = -1;
      result.errmsg = e.toString();
    }
    result.time = Date.now() - sT;
    return this.json(this.format(result));
  }

  /**
   * 上一篇下一篇
   * @returns {Promise<any>}
   */
  async prenextAction() {
    const sT = Date.now();
    const post = this.post();
    const result = {};
    const db = this.model("news");
    try {
      result.data =
        (await db
          .where(post.where)
          .order(post.order)
          .find()) || {};
      result.errno = 0;
    } catch (e) {
      result.errno = -1;
      result.errmsg = e.toString();
    }
    result.time = Date.now() - sT;
    return this.json(this.format(result));
  }

  async save({ content, ...form }) {
    const db = this.model("news");
    const dbContent = this.model("news_content");
    const id = await db.save({ ...form });
    if (!form.id) form.id = id;
    if (content) {
      await dbContent.saveByNewsId({ news_id: form.id, content });
    }
    return form.id;
  }
};
